/**
 * Class:DeleteCommentAction Class
 * Description:This ActionClass used for deletion of a particular comment
 */

package com.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.DAO.CommentDAO;
import com.DAO.NoticeDAO;
import com.beans.CommentForm;
import com.beans.NoticeForm;

public class DeleteCommentAction extends Action {
	private final static String SUCCESS = "success";

	/**
	 * This is the action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @throws java.lang.Exception
	 * @return
	 */
	@SuppressWarnings("finally")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("I am in deletion of comment......");
		
CommentDAO commentDao = new CommentDAO();
		
		int id = Integer.parseInt(request.getParameter("cid"));
		int deletedRows = 0;
		try {
			deletedRows = commentDao.delete(id);
		} catch (Exception e) {
			
			System.out.println("Error in comment delete action");
			e.printStackTrace();
		}
		
		 HttpSession session=request.getSession();
		    NoticeForm noticeForm=(NoticeForm)session.getAttribute("noticeform");
		    System.out.println("notice form is.........."+noticeForm);
		    
		    int notice_id=noticeForm.getNoticeId();
		    
		    NoticeDAO noticeDAO=new NoticeDAO();
		    try{
			//int id=Integer.parseInt(request.getParameter("notice_id"));
			
			ArrayList<CommentForm> listComments =new ArrayList<CommentForm>();
	    	listComments=noticeDAO.getComments(notice_id);
	    	request.setAttribute("listComments", listComments);
			}
			catch(Exception e)
			{
				System.out.println("Notice is is not found");
			}
			

			noticeDAO.closeConnection();
			commentDao.closeConnection();
			return mapping.findForward(SUCCESS);
		
	}
}	